#!/usr/bin/env python

import os
import re
import sys
import glob


def walk_through_data_dir(dir):
	data = []
	install_dir = dir

	if install_dir.startswith("icons"):
		install_dir = install_dir.replace("icons", "share/icons/hicolor")

	data.append((install_dir,glob.glob(dir + "/*.*")))
	for f in os.listdir(dir):
		path = os.path.join(dir, f)
		if os.path.isdir(path) and not path.find(".svn") > -1:
			for i in walk_through_data_dir(path):
				data.append(i)
	return data


def main():
	icon_files = walk_through_data_dir(".")

	print "hicolordir = $(datadir)/icons/hicolor\n"

	print "icons_FILES = \\"

	itr = 0
	num_total = len(icon_files)
	last = False

	for icon_ in icon_files:
		dir, icons = icon_

		itr = itr + 1
		if itr == num_total:
			last = True

		itr2 = 0
		num_total2 = len(icons)
		last2 = False

		for icon in icons:
			itr2 = itr2 + 1

			if icon.startswith("./"):
				icon = icon[2:]

			if icon.endswith("png") or icon.endswith("svg"):
				if itr2 == num_total2 and last == True:
					print "\t" + icon
				else:
					print "\t" + icon + " \\"


	print "\ninstall-data-local:"
	print "\tfor icon in $(icons_FILES); do \\"
	print "\t\tSIZE=`echo $$icon | cut -d/ -f2`; \\"
	print "\t\tFILE=`echo $$icon | cut -d/ -f3`; \\"
	print "\t\t$(mkinstalldirs) $(datadir)/icons/hicolor/$$SIZE/apps; \\"
	print "\t\t$(INSTALL_DATA) $(srcdir)/$$icon $(datadir)/icons/hicolor/$$SIZE/apps/$$FILE; \\"
	print "\tdone"
	#print "\t$(INSTALL_DATA) $(srcdir)/index.theme $(datadir)/icons/hicolor/"
	print "\ttouch $(hicolordir)"
	print "\ttouch $(datadir)/icons/hicolor\n"

	print "DISTCLEANFILES = Makefile.in\n"

	print "EXTRA_DIST = \\"
	print "\t$(icons_FILES)"

if __name__ == '__main__':
	main()
